
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_app/commom/Dio.dart';
import 'package:flutter_app/commom/Global.dart';
import 'package:flutter_app/widgets/HYSizeFit.dart';
import 'package:flutter_app/widgets/ReleasePjItems.dart';
import 'package:flutter_app/widgets/UndertakeItems.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

import 'ProjectInfo.dart';

class WaitingPage extends StatefulWidget{
  const  WaitingPage({
    Key key,
  }) : super(key: key);
  @override
  _WaitingPageState createState() => _WaitingPageState();
}
class _WaitingPageState extends State<WaitingPage> {

  _WaitingPageState();
  Future<String> _loadingFuture;

  @override
  void initState() {
    super.initState();
    _loadingFuture = loadingDataFunction();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<String>(
        //future：这个参数需要一个Future 对象，类似于 网络请求、IO
        future: _loadingFuture,
        ///这里的数据格式与上述FutureBuilder设置的泛型格式一至
        initialData: "初始化的数据",
        ///构建显示的 Widget  会根据加载的状态来多次回调些方法
        builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
          //加载状态判断
          switch (snapshot.connectionState) {

          ///可理解为初始加载显示的 Widget 异步加载开始时的回调
            case ConnectionState.none:
              return  Text('Result: ${snapshot.data}');
          ///异步加载中的回调
            case ConnectionState.active:
            case ConnectionState.waiting:
              return SpinKitRotatingPlain(
                color: Colors.black54,
              );
          ///异步加载完成的回调
            case ConnectionState.done:
            //判断是否加载失败
              if (snapshot.hasError) {
                return Text('Error: ${snapshot.error}');
              } else {
                ///通过 [snapshot.data]来获取异步加载的数据
                return OrderManagementRoute();
              }
          }
          return null;
        },
      ),
    );
  }

  ///模拟耗时的网络请求
  Future<String> loadingDataFunction() async {
    Global.findUserProjects = await EA(context).findUserProjects();
    Global.findUserProcedures = await EA(context).getUndertakeList();
  }
    ///模拟
}

class OrderManagementRoute extends StatefulWidget {
  OrderManagementRoute({
    Key key,
  }) : super(key: key);
  @override
  State<StatefulWidget> createState(){
    return OrderManagementRouteState();
  }
}
class OrderManagementRouteState extends State<OrderManagementRoute> {

  final List<String> _tabValues = [
    '我发布的',
    '我包揽的',
  ];

  TabController _controller;
  @override
  void initState() {
    super.initState();
    _controller = TabController(
      length: _tabValues.length,
      vsync: ScrollableState(),
    );
    //_onLoadingDate();
  }
  void _onLoadingDate()async{
    showLoading(context);
    try{
      Global.findUserProjects = await EA(context).findUserProjects();
      Global.findUserProcedures = await EA(context).getUndertakeList();
    } catch(e) {
      if (e.response?.statusCode == 401) {
        Navigator.of(context).pop();
      } else{
        Navigator.of(context).pop();
      }
    } finally {
      // 隐藏loading框
      Navigator.of(context).pop();
    }
  }
  void showLoading(context, [String text]) {
    text = text ?? "Loading...";
    showDialog(
        barrierDismissible: false,
        context: context,
        builder: (context) {
          return Center(
            child: Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(3.0),
                    boxShadow: [
                      //阴影
                      BoxShadow(
                        color: Colors.white,
                        //offset: Offset(2.0,2.0),
                        blurRadius: 10.0,
                      )
                    ]),
                padding: EdgeInsets.all(16),
                margin: EdgeInsets.all(16),
                constraints: BoxConstraints(maxHeight: 70, maxWidth: 70),
                child: SpinKitRotatingPlain(
                  color: Colors.black45,
                )
            ),
          );
        });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(239, 239, 239, 1),
      appBar: AppBar(
          backgroundColor: Colors.white,
          centerTitle: true,
        bottom: TabBar(
          tabs: _tabValues.map((f) {
            return Text(f);
          }).toList(),
          controller: _controller,
          indicatorColor: Colors.black,
          indicatorSize: TabBarIndicatorSize.label,
          isScrollable: false,
          labelColor: Colors.black,
          unselectedLabelColor: Colors.black54,
          indicatorWeight: 2.0,
          labelStyle: TextStyle(
            height: 2,
            fontSize: 17,
          ),
        ),
          title: Text('订单管理',
              style: TextStyle(
                //color: Colors.grey[700],
                fontSize: 20.0,
                height: 1.2,
              )),
          elevation: 0, //去掉Appbar底部阴影
          brightness: Brightness.light, //状态栏颜色
          leading: Builder(builder: (context) {
            return new IconButton(
              icon: new Icon(
                Icons.arrow_back_ios,
                color: Colors.grey,
              ),
              tooltip: '退出',
              onPressed: () {
                //返回
                Navigator.of(context).pop(this);
              },
            );
          })
      ),
        body: TabBarView(
          controller: _controller,
          children: [
            ReleasePjItems(),
            UndertakeItems(),
          ]
        ),
    );
  }

}